﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
namespace SQT.tracnghiem.Ajax.ASP.News
{
    public partial class add_News_detail : System.Web.UI.Page
    {
        public int last;
        protected void Page_Load(object sender, EventArgs e)
        {
            int cate = int.Parse(Request.Form["cate"]);
            string title = Request.Form["title"];
            string review = Request.Form["review"];
            string content = Request.Form["content"];
            string source = Request.Form["source"];
            string tags = Request.Form["tags"];
            string img = Request.Form["img"];
            int show = int.Parse(Request.Form["show"]);
            int top = int.Parse(Request.Form["top"]);

            bool bool_top = false, bool_show=false;
            if(top==1)
                bool_top= true;
            if(show==1)
                bool_show = true;

            string tags_id = "";
            if (tags != "")
            {
                int checkSplit = tags.IndexOf(',');
                if (checkSplit == -1)
                {
                    tags_id = checkTag(convertToUrl(tags), tags);
                }
                else
                {
                    string[] split = new string[tags.Split(',').Length];
                    split = tags.Split(',');
                    
                    for (int i = 0; i < split.Length; i++)
                    {
                        if (string.IsNullOrEmpty(split[i].Trim()))
                            continue;
                        else
                        {
                            if (i>0&&i < split.Length &&tags_id!="")
                                tags_id += ",";
                            tags_id += checkTag(convertToUrl(split[i].Trim()), split[i].Trim());
                            
                        }
                    }
                }
            }
            string[] split_img = new string[img.Split('\\').Length];
            split_img = img.Split('\\');
            img = Session["admin_id"].ToString() + "_" + DateTime.Now.Day.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Year.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + split_img[img.Split('\\').Length-1];

            new TblNewsController().Insert(title, review, content, int.Parse(Session["admin_id"].ToString()), tags_id, cate, source,img, bool_top, false, bool_show, 0, 0, 0, DateTime.Now);
        }
        public static string convertToUnSign(string s)
        {
            string convert = "ĂÂÀẰẦÁẮẤẢẲẨÃẴẪẠẶẬỄẼỂẺÉÊÈỀẾẸỆÔÒỒƠỜÓỐỚỎỔỞÕỖỠỌỘỢƯÚÙỨỪỦỬŨỮỤỰÌÍỈĨỊỲÝỶỸỴĐăâàằầáắấảẳẩãẵẫạặậễẽểẻéêèềếẹệôòồơờóốớỏổởõỗỡọộợưúùứừủửũữụựìíỉĩịỳýỷỹỵđ";
            string To = "aaaaaaaaaaaaaaaaaeeeeeeeeeeeooooooooooooooooouuuuuuuuuuuiiiiiyyyyydaaaaaaaaaaaaaaaaaeeeeeeeeeeeooooooooooooooooouuuuuuuuuuuiiiiiyyyyyd";
            for (int i = 0; i < To.Length; i++)
            {
                s = s.Replace(convert[i], To[i]);
            }
            return s;
        }
        static string checkTag(string s, string name)
        {
            var get_tag = new TblNewsTagController().FetchAll().Where(x => x.TagUrl == s).ToList();
            string id = "";
            if (get_tag.Count != 0)
            {
                id = get_tag[0].TagId.ToString();
            }
            else
            {
                new TblNewsTagController().Insert(name, s);
                var get_last = new TblNewsTagController().FetchAll().Last();
                id = get_last.TagId.ToString();
            }
            return id;
        }
        static string convertToUrl(string s)
        {
            string url = "";
            s = s.ToLower();
            if (s.IndexOf(' ') != -1)
            {
                string[] split = new string[s.Split(' ').Length];
                split = s.Split(' ');
                
                for (int i = 0; i < split.Length; i++)
                {
                    url += convertToUnSign(split[i].Trim());
                    if (i < split.Length - 1)
                        url += "-";
                }
                s = url;
            }
            else
            {
                url += convertToUnSign(s);
            }
            s = url;
                return s;
        }
    }
}